package com.njitzx.controller;


import com.njitzx.entity.po.Attribute;
import com.njitzx.entity.vo.AttributeVO;
import com.njitzx.service.AttributeService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.njitzx.entity.vo.Result;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;

import lombok.RequiredArgsConstructor;


import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 * 属性表 前端控制器
 * </p>
 *
 * @author 周鑫
 * @since 2024-08-20
 */
@Api(tags = "属性表接口")
@RestController
@RequestMapping("/attributes")
@RequiredArgsConstructor
public class AttributeController {

    private final AttributeService attributeService;

//    @ApiOperation("属性表分页查询")
//    @GetMapping("/list")
//    public Result findPage(@ApiParam("页码") @RequestParam(defaultValue = "1") Integer pageNum,
//                           @ApiParam("页大小") @RequestParam(defaultValue = "5") Integer pageSize,
//                           @ApiParam("查询条件") Attribute attribute) {
//        PageInfo<Attribute> pageInfo = attributeService.findPage(pageNum, pageSize, attribute);
//        return Result.success(pageInfo);
//    }

    @ApiOperation("根据分类查询参数列表")
    @GetMapping("/category/{catId}")
    public Result findById(@PathVariable("catId") Integer catId, @RequestParam("sel") String sel) {
        return Result.success(attributeService.findById(catId, sel));
    }
    @ApiOperation("根据id查询参数ok")
    @GetMapping("/{attrId}")
    public Result findById(@PathVariable("attrId") Integer attrId) {
        return Result.success(attributeService.selectByattrId(attrId));
    }
    @ApiOperation("添加动态参数或者静态属性")
    @PostMapping("/{catId}")
    public Result add(@PathVariable("catId") Integer catId, @RequestBody Attribute attribute) {
        System.out.println(attribute);
         attribute.setCatId(catId);
        attributeService.add(attribute);
        return Result.success();
    }

//    @ApiOperation("修改属性表")
//    @PutMapping
//    public Result modify(@RequestBody Attribute attribute) {
//        attributeService.modify(attribute);
//        return Result.success();
//    }

    @ApiOperation("根据主键删除属性表")
    @DeleteMapping("/{attrId}")
    public Result removeById(@PathVariable Serializable attrId) {
        attributeService.removeById(attrId);
        return Result.success();
    }

    @ApiOperation("更新参数及明细")
    @PutMapping("/{attrId}")
    public Result update(@PathVariable("attrId")Integer attrId ,@RequestBody Attribute attribute) {
        attribute.setAttrId(attrId);
        AttributeVO attributeVO= attributeService.modify(attribute);
        return Result.success(attributeVO);
    }

}
